package com.wugh.fast.demo.controller;

import com.wugh.fast.demo.consumer.BTestConsumer;
import com.wugh.fast.demo.service.TestService;
import com.wugh.fast.module.api.vo.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.concurrent.ExecutionException;

/**
 * 说明:
 *
 * @author wugh
 * @date 2019-09-17 11:37
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private BTestConsumer bTestConsumer;
    @Autowired
    private TestService testService;

    @GetMapping("error")
    public ResponseVO<Integer> error() {
        return ResponseVO.ok(100 / 0);
    }

    @GetMapping
    public ResponseVO<Date> test() throws ExecutionException, InterruptedException {
        //异步调用
//        Future<ResponseVO<Object>> future = ThreadUtil.execAsync(ContextAwareCallable.build(() -> {
//            ResponseVO<Object> mine = bTestConsumer.getMine();
//            System.out.println(mine);
//            return mine;
//        }));
        //同步调用,但会用到hystrix线程
        bTestConsumer.getMine();
        return ResponseVO.ok(new Date());
    }

    @PostMapping("upload")
    public ResponseVO<Date> upload(String str, MultipartFile file) {
        log.info("str:{},file:{},{},{}", str, file.getOriginalFilename(), file.getContentType(), file.getSize());
        return ResponseVO.ok(new Date());
    }
}
